start:
  ldr OPB, #0b00111000       ; set 8-bit mode, 2-line display, 5x8 font
  call lcd_instruction
  
  ldr OPB, #0b00001110       ; set display on, curson on, blink off 
  call lcd_instruction

  ldr OPB, #0b00000110       ; increment and shift cursor
  call lcd_instruction

  ldr OPB, #0b00000001       ; clear display
  call lcd_instruction

print_message:
  ldr D, message             ; address of 'message' is now in register D

print_message_loop:
  ldm $D, A                  ; load value in memory at address in reg. D into reg. A
  
  cmp #0                     ; compare A with 0
  jmz end                    ; jump to 'end' if A == 0

  mov A, OPB                 ; move reg. A to reg. Output Port B
  call lcd_character

  inc D                      ; increment reg. D

  jump print_message_loop    ; jump back to the start of the loop

end:
  hlt

lcd_instruction:
  ldr OPA, #0b00000000       ; clear RS/RW/EN bits
  ldr OPA, #0b10000000       ; toggle enable pin
  ldr OPA, #0b00000000       ; clear RS/RW/EN bits
  ret

lcd_character:
  ldr OPA, #0b00100000       ; set RS bit
  ldr OPA, #0b10100000       ; toggle enable pin
  ldr OPA, #0b00100000       ; clear enable bit
  ret

message: asciiz "Hello, world!"     ; message to be displayed, ended with 0x00 in ram






